package com.zhongwang.mms.module.allocation.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.AllocationOrderDetail;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDao;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDetailDao;
import com.zhongwang.mms.module.allocation.model.AllocationOrderModel;
import com.zhongwang.mms.module.half.service.CreateOrderCodeService;
import com.zhongwang.mms.module.material.dao.ComplexStorageDao;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.purchase.dao.PurPInventoryDao;
import com.zhongwang.mms.module.purchase.model.PInventoryModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description:调拨单管理
 * @author: th
 * @time: 2020/8/11 10:37
 */
@Service
public class AllocationOrderService {

    @Autowired
    private AllocationOrderDao allocationOrderDao;
    @Autowired
    private AllocationOrderDetailDao allocationOrderDetailDao;
    @Autowired
    private CreateOrderCodeService createOrderCodeService;
    @Autowired
    private PurPInventoryDao purPInventoryDao;
    @Autowired
    private ComplexStorageDao complexStorageDao;
    @Autowired
    private LogService logService;

    /**
     * 根据条件查询调拨单
     * @param query
     * @return
     */
    public List<AllocationOrderModel> selectAllByQuery(AllocationOrderModel query) {

        return allocationOrderDao.selectAllByQuery(query);
    }

    /**
     * 创建调拨单保存
     * @param param
     * @return
     */
    public JsonResult saveAllocationOrder(AllocationOrderModel param) {
        User user = ShiroUtils.getUser();
        param.setOdrCode(createOrderCodeService.getAllocationOrderCode());
        param.setOdrMakeDate(new Date());
        param.setOdrMakePersonId(user.getName());
        param.setOdrStatus(1);
        param.setOdrOutStatus(1);
        logService.saveLog("创建调拨单","调拨单号："+param.getOdrCode());
        return JsonResult.success().setData(allocationOrderDao.insertSelective(param));
    }

    /**
     * 根据主表ID查询调拨单明细
     * @param ardOdrId
     * @return
     */
    public List<AllocationOrderModel> selectOrderDetailByArdOdrId(Integer ardOdrId) {

        return allocationOrderDetailDao.selectAllByArdOderId(ardOdrId);
    }

    /**
     * 根据条件查询外购件库存
     * @param param
     * @return
     */
    public List<PInventoryModel> selectPurInvList(PInventoryModel param) {

        return purPInventoryDao.findInventoryList(param);
    }

    /**
     * 保存调拨单明细
     * @param param
     * @return
     */
    public JsonResult saveDetail(List<AllocationOrderModel> param) {

        AllocationOrderDetail allocationOrderDetail = new AllocationOrderDetail();

        for (AllocationOrderModel model : param){

            allocationOrderDetail.setArdOdrId(model.getOdrId());
            if(model.getMatNameDesc() ==null){
                allocationOrderDetail.setArdMatName(model.getItyMatDesc()==null?model.getItyMatName():model.getItyMatDesc());
            }else {
                allocationOrderDetail.setArdMatName(model.getMatNameDesc());
            }
            allocationOrderDetail.setArdSapCode(model.getItySapCode()==null?model.getItyMatSapCode():model.getItySapCode());
            allocationOrderDetail.setArdProCode(model.getItyProCode());
            allocationOrderDetail.setArdProName(model.getItyProName());
            allocationOrderDetail.setArdPart(model.getItyPart());
            allocationOrderDetail.setArdTrackCode(model.getItyTrackCode());
            if(model.getItyStockType() != null){
                allocationOrderDetail.setArdStockType(model.getItyStockType());//在库类型
            }else {
                allocationOrderDetail.setArdStockType(model.getItyInType());
            }
            allocationOrderDetail.setArdSpecialRemark(model.getItyRemark()==null?model.getItySpec():model.getItyRemark());
            allocationOrderDetail.setArdOutNum(model.getItyNum());
            allocationOrderDetail.setArdOutWeight(model.getItyWeight());
            allocationOrderDetail.setArdPrice(model.getItyPrice());
            allocationOrderDetail.setArdUnit(model.getItyNumUnit());
            allocationOrderDetail.setArdOutItyId(model.getItyId());
            allocationOrderDetail.setArdMatType(model.getMatTypeName());
            allocationOrderDetail.setArdMatQuality(model.getMatQualityName());
            allocationOrderDetail.setArdMatSpec(model.getMatSpecName());
            allocationOrderDetail.setArdMatWidth(model.getMatWidth());
            allocationOrderDetail.setArdMatLength(model.getMatLength());
            allocationOrderDetail.setArdMatHeight(model.getMatHeight());
            allocationOrderDetail.setArdDataStatus(1);

            allocationOrderDetailDao.insertSelective(allocationOrderDetail);
        }

        return JsonResult.success();
    }

    /**
     * 修改调拨单明细数量
     * @param param
     * @return
     */
    public int updateAllocationOrderDetail(AllocationOrderModel param) {

        return allocationOrderDetailDao.updateByPrimaryKeySelective(param);
    }

    /**
     * 删除调拨单明细
     * @param ardId
     * @return
     */
    public int delAllocationOrderDetail(Integer ardId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setArdDataStatus(0);
        allocationOrderModel.setArdId(ardId);
        return allocationOrderDetailDao.updateByPrimaryKeySelective(allocationOrderModel);
    }

    /**
     * 提交调拨单
     * @param odrId
     * @return
     */
    public int commitAllocationOrderByPrimaryKey(Integer odrId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setOdrStatus(2);
        allocationOrderModel.setOdrId(odrId);

        return allocationOrderDao.updateByPrimaryKeySelective(allocationOrderModel);
    }

    /**
     * 删除调拨单
     * @param odrId
     * @return
     */
    public int delAllocationOrderByPrimaryKey(Integer odrId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setOdrStatus(0);
        allocationOrderModel.setOdrId(odrId);

        AllocationOrderModel model = allocationOrderDao.selectAllByPrimaryKey(odrId);
        logService.saveLog("删除调拨单","调拨单号:"+model.getOdrCode());
        return allocationOrderDao.updateByPrimaryKeySelective(allocationOrderModel);
    }

    /**
     * 撤销调拨单提交
     * @param odrId
     * @return
     */
    public int rejectAllocationOrderByPrimaryKey(Integer odrId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setOdrStatus(1);
        allocationOrderModel.setOdrId(odrId);

        return allocationOrderDao.updateByPrimaryKeySelective(allocationOrderModel);
    }

    /***
     * 原料库存
     * @param param
     * @return
     */
    public List<ComplexStorageModel> selectMatInvList(ComplexStorageModel param) {

        return complexStorageDao.findComplexStorage(param);
    }

    /**
     * 调拨查询页面数据
     * @param query
     * @return
     */
    public List<AllocationOrderModel> selectOrderDetailByQuery(AllocationOrderModel query) {

        return allocationOrderDetailDao.selectOrderDetailByQuery(query);
    }

    /**
     * 调拨审核页面数据
     * @param query
     * @return
     */
    public List<AllocationOrderModel> selectAllCheckByQuery(AllocationOrderModel query) {

        return allocationOrderDao.selectAllCheckByQuery(query);
    }

    /**
     * 调拨单审核通过
     * @param odrId
     * @return
     */
    public int commitAllocationCheckByPrimaryKey(Integer odrId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setOdrStatus(3);
        allocationOrderModel.setOdrId(odrId);
        AllocationOrderModel model = allocationOrderDao.selectAllByPrimaryKey(odrId);
        logService.saveLog("调拨单审核","审核通过 调拨单号："+model.getOdrCode());
        return allocationOrderDao.updateByPrimaryKeySelective(allocationOrderModel);
    }

    /**
     * 调拨单审核驳回
     * @param odrId
     * @return
     */
    public int rejectAllocationCheckByPrimaryKey(Integer odrId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setOdrStatus(4);
        allocationOrderModel.setOdrId(odrId);
        AllocationOrderModel model = allocationOrderDao.selectAllByPrimaryKey(odrId);
        logService.saveLog("调拨单审核","驳回 调拨单号："+model.getOdrCode());
        return allocationOrderDao.updateByPrimaryKeySelective(allocationOrderModel);
    }

    /**
     * 撤销审核通过
     * @param odrId
     * @return
     */
    public int revokeAllocationCheckByPrimaryKey(Integer odrId) {

        AllocationOrderModel allocationOrderModel = new AllocationOrderModel();
        allocationOrderModel.setOdrStatus(2);
        allocationOrderModel.setOdrId(odrId);

        AllocationOrderModel model = allocationOrderDao.selectAllByPrimaryKey(odrId);
        logService.saveLog("调拨单审核","撤销 调拨单号："+model.getOdrCode());
        return allocationOrderDao.updateByPrimaryKeySelective(allocationOrderModel);
    }
}
